
/****************************************************************************************
*                                                                                      *
*                          Projeto BioInfo - Lincs and CIn / UFPE                      *
*                                    07/10/2014                                        *
*																					   *
****************************************************************************************
* Descricao: Arquivo que contem funcao run_ocl                                *
****************************************************************************************
* Responsaveis: Jefferson Ramos(jrla)													   *
*                                                                                      */
/*                                                                                     *
****************************************************************************************/

#include <bioaplication_sw.h>

void bioaplication_sw::run_ocl(){

	fopen_s(&bioinfo_ocl->TaxaOut, "../files/ocl_TaxaOut.txt", "w+");

	printf("\nPress ENTER to run Ocl:\n");
	fflush(stdin);
	getchar();

	fopen_s(&bioinfo_ocl->BD_nucleotideos, "../files/BD_out_bin.bin", "rb");
	if (bioinfo_ocl->BD_nucleotideos == NULL){
		printf("\nError: No open file canonic_BD_nucleotideos\n");
		fflush(stdin);
		getchar();
	}

	TictocAverage tt;

	// OpenCL initialization
	if (bioinfo_ocl->initializeCL(use_gpu)){ printf("ERROR: initializeOpenCl\n"); exit(-1); }


	// Kernel initialization
	if (bioinfo_ocl->initializeKernel()){ printf("ERROR: initializeKernel\n"); }

	num_individuals = bioinfo_ocl->get_num_individuals();
	int max_individuals = num_individuals;
	int alocation_max_ind = 200000;

	int m = num_individuals / alocation_max_ind;

	if (num_individuals >= alocation_max_ind)
		num_individuals = alocation_max_ind;
	else
		alocation_max_ind = num_individuals;

	//passando numero maximo de individuos para objeto do Ocl
	bioinfo_ocl->set_num_individuals(alocation_max_ind);

	// Vectors Allocation
	if (bioinfo_ocl->vectors_malloc()){ printf("ERROR: vectors_malloc\n"); exit(-1); }

	//Read Query input Files and Rewind File
	bioinfo_ocl->read_input_query();
	rewind(bioinfo_ocl->BD_nucleotideos);

	// Buffers initialization
	if (bioinfo_ocl->initializeCLBuffers()){ printf("ERROR: initializeCLBuffers\n"); exit(-1); }

	//Cl Set Args of Kernel
	bioinfo_ocl->setKernelArgs();

	printf("\nStart Ocl Run_Times: %d\n", run_times);

	for (int j = 0; j < run_times; j++){
		//initializing sum_time
		bioinfo_ocl->sum_time = 0.0;

		//passando numero maximo de individuos para objeto do Ocl
		bioinfo_ocl->set_num_individuals(alocation_max_ind);

		for (int i = 0; i < m; i++){

			bioinfo_ocl->read_input_individuals();//Read Individuals input Files

			tt.Tic();
			if (bioinfo_ocl->runCLKernels()){ printf("ERROR: runCLBuffers\n"); exit(-1); }// Kernel Run

			bioinfo_ocl->sum_time += tt.Toc();

#ifdef PRINT_TAXA
			bioinfo_ocl->print_results();
#endif
		}
		if ((max_individuals - alocation_max_ind*m) > 0){

			//passando numero maximo de individuos para objeto do Ocl
			bioinfo_ocl->set_num_individuals(max_individuals - alocation_max_ind*m);

			bioinfo_ocl->read_input_individuals();//Read Individuals input Files

			tt.Tic();
			if (bioinfo_ocl->runCLKernels()){ printf("ERROR: runCLBuffers\n"); exit(-1); } // Kernel Run
			bioinfo_ocl->sum_time += tt.Toc();

#ifdef PRINT_TAXA
			bioinfo_ocl->print_results();
#endif
		}

		bioinfo_ocl->time_average += bioinfo_ocl->sum_time;
		printf("Rodada: %d Tempo: %.2f ms\n", j + 1, bioinfo_ocl->sum_time * 1000);

		rewind(bioinfo_ocl->BD_nucleotideos);
#ifdef PRINT_TAXA
		rewind(bioinfo_ocl->TaxaOut);
#endif
	}

	printf("\nTempo medio: %.2f ms\n\n", bioinfo_ocl->time_average * 1000 / run_times);
	printf("\nPress ENTER to exit Ocl:\n");
	fflush(stdin);
	getchar();

	if (bioinfo_ocl->shutdown()) { printf("ERROR: shutdown\n"); exit(-1); }; // OpenCL shutdown

	fclose(bioinfo_ocl->BD_nucleotideos);//fechando arquivo BD
}

